package com.liliya.servlet.order;

import com.liliya.pojo.Order;
import com.liliya.service.OrderService;
import com.liliya.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Description TODO
 * @Author hhy
 * @Date 2021/2/21 9:16
 */
@WebServlet(urlPatterns = "/order/listok")
public class OrderListOKServlet extends HttpServlet {
    private OrderService orderService = new OrderServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int step = 3;
        //一共有多少页
        Integer total = orderService.findPageOK(step);

        int num = 1;
        String pageNum = req.getParameter("p");
        if (pageNum != null) {
            num = Integer.parseInt(pageNum);
        }
        int start = (num - 1) * step + 1;
        int end = num * step;
        //上一页和下一页
        int pre = num - 1 < 1 ? 1 : num - 1;
        int next = num + 1 > total ? total : num + 1;

        List<Order> orders = orderService.findOrdersOKByRow(start, end);
        req.setAttribute("total",total);
        req.setAttribute("pre",pre);
        req.setAttribute("next",next);


        req.setAttribute("orders",orders);
        req.getRequestDispatcher("./orderlistok.jsp").forward(req,resp);
//        resp.sendRedirect(req.getContextPath()+"/order/orderlist.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
